package main

import (
	uploader2 "client/uploader"
	"client/utils"
	"context"
	"flag"
	"fmt"
	"net/http"
	"time"
)

var (
	uploadFilePath = flag.String("uploadFilePath", "", "上传文件在本地的路径")
)

func handleUpload() {
	client := http.Client{}
	c, cancel := context.WithTimeout(context.Background(), time.Minute*10)
	filePath := *uploadFilePath

	info, err := utils.FileExist(filePath)
	if err != nil {
		fmt.Println(err)
		cancel()
		return
	}

	// 文件大小小于FileMaxSize时，直接上传
	if info.Size() < FileMaxSize {
		uploader := uploader2.NewDirectUploader(filePath, client)
		uploader.Upload(c)
		cancel()
		return
	}
	// 文件大小大于FileMaxSize时，需要分片上传
	config := uploader2.FragmentUploadConfig{
		FileMetaDir: FileMetaDir,
		SliceSize:   SliceSize,
		FileMaxSize: FileMaxSize,
		FilePath:    *uploadFilePath,
	}
	uploader := uploader2.NewFragmentUploader(config, client)
	uploader.HandleUpload(c)
	cancel()
}

func main() {
	flag.Parse()
	handleUpload()
}
